#ifndef RMFF_H
#define RMFF_H

#include <pspkernel.h>
#include <stdint.h>
#include "codec.h"
#include "ffmpeg/config.h"

#define RM_HEADER_OBJECT				0x2E524D46		// '.RMF'
#define RM_PROPERTIES_OBJECT			0x50524F50		// 'PROP'
#define RM_MEDIA_PROPERTIES_OBJECT		0x4D445052		// 'MDPR'
#define RM_CONTENT_OBJECT				0x434F4E54		// 'CONT'
#define RM_DATA_OBJECT					0x44415441		// 'DATA'
#define RM_INDEX_OBJECT					0x494E4458		// 'INDX'

#define RA_FOURCC						0x2E7261FD		// '.ra'0xFD
#define AAC_FOURCC_1					0x72616163		// 'raac'
#define AAC_FOURCC_2					0x72616370		// 'racp'

#define RV_FOURCC						0x5649444F		// 'VIDO'
#define RV10_FOURCC						0x52563130		// 'RV10'
#define RV20_FOURCC						0x52563230		// 'RV20'
#define RV30_FOURCC						0x52563330		// 'RV30'
#define RV40_FOURCC						0x52563430		// 'RV40'

typedef enum stream_type_t {
	STREAM_TYPE_UNKNOWN = 0,
	STREAM_TYPE_AUDIO,
	STREAM_TYPE_VIDEO
} stream_type_t;

typedef struct rmff_object_t {
	uint32_t			id;
	uint32_t			size;
	uint16_t			version;
} rmff_object_t;

typedef struct rmff_cont_t {
	uint16_t			title_len;
	char				*title;
	uint16_t			author_len;
	char				*author;
	uint16_t			copyright_len;
	char				*copyright;
	uint16_t			comment_len;
	char				*comment;
} rmff_cont_t;

typedef struct rmff_stream_t {
	uint32_t			id;
	int					present;
	stream_type_t		type;
	struct rmff_queue_t	*queue;
	char				*name;
	char				*mime;
	codec_t				codec;
	uint32_t			avg_bit_rate;
//audio
	uint16_t			sample_rate;
	uint16_t			sample_size;
	uint16_t			channels;
//video
	uint16_t			width;
	uint16_t			height;
	uint16_t			fps;
} rmff_stream_t;

typedef struct rmff_packet_t {
	uint32_t			length;
	rmff_stream_t		*stream;
	uint32_t			timestamp;
	void				*data;
	void				*heap_internal;
} rmff_packet_t;

typedef struct rmff_queue_t {
	rmff_packet_t		**storage;
	uint32_t			head;
	uint32_t			tail;
	uint32_t			capacity;
	uint32_t			num_packets;
	int					get_sleep;
	int					put_sleep;
	SceUID				get_tid;
	SceUID				put_tid;
} rmff_queue_t;

typedef struct rmff_file_t {
	SceUID				file_handle;
	uint32_t			duration;
	uint32_t			index_offset;
	uint32_t			data_offset;
	uint16_t			num_streams;
	int 				cont_header_present;
	rmff_cont_t			cont_header;
	rmff_stream_t		streams[max_streams];
	int					fault;
} rmff_file_t;

int rmff_open_file(char*, rmff_file_t*);
void rmff_close_file(rmff_file_t*);
int rmff_read_headers(rmff_file_t*);
rmff_packet_t* rmff_read_packet(rmff_file_t*);
void rmff_release_packet(rmff_packet_t*);

#endif
